package javango.polls.forms;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.google.inject.Inject;

import javango.contrib.compat.Javango;
import javango.contrib.hibernate.ModelChoiceField;
import javango.contrib.hibernate.annotations.ModelChoiceFieldProperties;
import javango.db.ManagerException;
import javango.forms.AbstractForm;
import javango.forms.fields.FieldFactory;
import javango.forms.fields.annotations.FieldProperties;
import javango.polls.model.Choice;
import javango.polls.model.Poll;

public class VoteForm extends AbstractForm {

	@Inject
	public VoteForm(FieldFactory fieldFactory, Javango javango) {
		super(fieldFactory);
		this.javango = javango;
	}

	Javango javango;

	Log log = LogFactory.getLog(VoteForm.class);

	
	@FieldProperties(verboseName="Choice")
	@ModelChoiceFieldProperties(modelClass=Choice.class)
	public ModelChoiceField<Choice> choice;
	
	public VoteForm setPoll(Poll poll) {
		getFields();
		try {
			choice.setQuerySet(javango.newManager(Choice.class).filter("poll", poll));
		} catch (ManagerException e) {
			LogFactory.getLog(VoteForm.class).error(e,e);
		}
		return this;
	}

}
